<?php
namespace Infinity;

abstract class Mail {
  public static function send($module, $key, $from, $to, $subject, $body, $params=array()) {
    $language = language_default();
    $send = FALSE;
    $message = drupal_mail($module, $key, $to, $language, $params, $from, $send);

    $message['subject'] = $subject;
    $message['body'] = array($body);

    $system = drupal_mail_system($module, $key);
    $message = $system->format($message);
    $message['result'] = $system->mail($message);
    return $message['result'];
  }

  public static function sendTo($module, $key, $to, $params=array()) {
    $from = variable_get('site_mail', ini_get('sendmail_from'));
    return drupal_mail($module, $key, $to, language_default(), $params, $from, TRUE);
  }

  public static function sendToRole($module, $key, $role, $params=array()) {
    $from = variable_get('site_mail', ini_get('sendmail_from'));
    $users = User::getUsersByRole($role);
    $results = array();
    foreach($users as $user) {
      $results []= drupal_mail($module, $key, $user->mail, language_default(), $params, $from, TRUE);
    }
    return $results;
  }
}